/*
 * Copyright 2020 DuJiang.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.amorou.rest.crypto.util;

import java.nio.charset.Charset;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;

import javax.crypto.Cipher;

import cn.amorou.rest.crypto.exception.DecryptException;
import cn.amorou.rest.crypto.exception.EncryptException;

/**
 * RSAUtils
 * 
 * @author DuJiang
 */
public final class RSAUtils {

	private static final String ALGORITHM = "RSA";

	/**
	 * 加密
	 * @param encryptText 需要加密的信息
	 * @param publicKey 公钥字符串
	 * @param charsetName 字符集名称
	 * @return String  加密后Base64编码的字符串
	 */
	public static final String encrypt(final String encryptText, final String publicKey, final String charsetName) {
		return encrypt(encryptText, KeyUtils.getPublicKey(ALGORITHM, publicKey), Charset.forName(charsetName));
	}

	/**
	 * 加密
	 * @param encryptText 需要加密的信息
	 * @param publicKey 公钥
	 * @param charsetName 字符集名称
	 * @return String  加密后Base64编码的字符串
	 */
	private static final String encrypt(final String encryptText, final PublicKey publicKey, final Charset charset) {
		if (encryptText == null) {
			throw new EncryptException("encryptText must not be null");
		}

		try {
			Cipher cipher = Cipher.getInstance(ALGORITHM);
			cipher.init(Cipher.ENCRYPT_MODE, publicKey);

			byte[] bs = cipher.doFinal(encryptText.getBytes(charset));
			return Base64.getEncoder().encodeToString(bs);
		} catch (Exception e) {
			throw new EncryptException("encrypt failed", e);
		}
	}

	/**
	 * 解密
	 * @param decryptText 需要解密的信息
	 * @param privateKey 私钥字符串
	 * @param charsetName 字符集名称
	 * @return String 解密后的字符串
	 */
	public static final String decrypt(final String decryptText, final String privateKey, final String charsetName) {
		return decrypt(decryptText, KeyUtils.getPrivateKey(ALGORITHM, privateKey), Charset.forName(charsetName));
	}

	/**
	 * 解密
	 * @param decryptText 需要解密的信息
	 * @param privateKey 私钥
	 * @param charsetName 字符集名称
	 * @return String 解密后的字符串
	 */
	private static final String decrypt(final String decryptText, final PrivateKey privateKey, final Charset charset) {
		if (decryptText == null) {
			throw new EncryptException("decryptText must not be null");
		}

		try {
			Cipher cipher = Cipher.getInstance(ALGORITHM);
			cipher.init(Cipher.DECRYPT_MODE, privateKey);

			byte[] bs = cipher.doFinal(Base64.getDecoder().decode(decryptText));
			return new String(bs, charset);
		} catch (Exception e) {
			throw new DecryptException("decrypt failed", e);
		}
	}

}